package com.xiaoxie.system.config;



import com.xiaoxie.common.constant.Constants;
import com.xiaoxie.system.datasource.DruidDataSourceBuilder;
import com.xiaoxie.system.datasource.DynamicDataSourceManager;
import com.alibaba.druid.pool.DruidDataSource;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * druid配置
 */
@Configuration
public class DataSourceConfig {

    @Resource
    private DataSourceProperties dataSourceProperties;



    // @Bean("defaultDataSource")
    public DruidDataSource defaultDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        return DruidDataSourceBuilder.build(dataSourceProperties,datasource);
    }

    @Bean
    public DynamicDataSourceManager dynamicDataSource() {
        Map<Object,Object> targetDataSources = new HashMap<>();
        DruidDataSource dateSource = defaultDataSource();
        targetDataSources.put(Constants.DEFAULT_DATASOURCE_NAME, dateSource);
        return new DynamicDataSourceManager(dateSource, targetDataSources);
    }

}
